import type { Directive } from "vue";

const debounce: Directive = {
  mounted(el, binding) {
    let timer: ReturnType<typeof setTimeout> | null = null;
    const delay = binding.arg ? Number(binding.arg) : 500;

    const handler = binding.value;
    el.addEventListener("click", () => {
      if (timer) return;
      timer = setTimeout(() => {
        timer = null;
      }, delay);
      handler();
    });
  }
};

export default debounce;
